unit FormTreatyManager;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxTextEdit,
    cxInplaceContainer, cxControls, StdCtrls, ExtCtrls, ActnList, Doc2Word;

type
    TfrmTreatyManager = class(TForm)
        Panel1: TPanel;
        Panel2: TPanel;
        bCreateTreaty: TButton;
        bClose: TButton;
        ActionList1: TActionList;
        actCreateTreaty: TAction;
        lbReport: TListBox;
        procedure bCloseClick(Sender: TObject);
        procedure actCreateTreatyExecute(Sender: TObject);
        procedure actCreateTreatyUpdate(Sender: TObject);
    procedure lbReportDblClick(Sender: TObject);
    private
    public
    { Public declarations }
        constructor Create(AOwner: TComponent); reintroduce;
        procedure LoadListTreaty;
    end;

var
    frmTreatyManager: TfrmTreatyManager;
    wa: TWordApp;

implementation

{$R *.dfm}

uses Templates, DataModule, Utils;

{ TfrmTreatyManager }

constructor TfrmTreatyManager.Create(AOwner: TComponent);
begin
    inherited Create(AOwner);
    LoadListTreaty;
end;

procedure TfrmTreatyManager.bCloseClick(Sender: TObject);
begin
    Close;
end;

procedure TfrmTreatyManager.LoadListTreaty;
var
    strPathTemplates: string;
    srRep: TSearchRec;
begin
    try
        lbReport.Clear;
        strPathTemplates := GetAgreementPath();
        if FindFirst(strPathTemplates + '*.do*', faAnyFile, srRep) = 0 then begin
            repeat
               lbReport.Items.Add(srRep.Name);
            until FindNext(srRep) <> 0;
            FindClose(srRep);
        end;
    except
        on E: exception do WriteLog(E.Message);
    end;
end;

procedure TfrmTreatyManager.actCreateTreatyExecute(Sender: TObject);
begin
    try
        wa := LoadTemplate(wa, lbReport.Items[lbReport.ItemIndex]);
        FirmInfoTemplate(DM.Conection, wa);
    except
        on E: exception do WriteLog(E.Message);
    end;
end;

procedure TfrmTreatyManager.actCreateTreatyUpdate(Sender: TObject);
begin
    TAction(Sender).Enabled :=lbReport.ItemIndex > -1;
end;

procedure TfrmTreatyManager.lbReportDblClick(Sender: TObject);
begin
   actCreateTreaty.Execute();
end;

end.

